/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class Author {

    private String name;

    private String uri;

    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public static Author marshalFrom(KXmlParser parser) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }
        Author author = new Author();
        try {
            parser.require(XmlPullParser.START_TAG, null, "author");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if (tagName.equals("name")) {
                    author.setName(parser.nextText());
                } else if(tagName.equals("email")) {
                   author.setEmail(parser.nextText());
                } else if(tagName.equals("uri")) {
                   author.setUri(parser.nextText());
                }
            }
        } catch (XmlPullParserException e) {
            throw new IOException("SLIDE-000-010:" + e.toString());
        }

        if(author.getName() == null) {
            throw new IOException("Missing required name tag");
        }
        return author;
    }    
}
